/**
 * 
 */
package org.intelligentsia.artifacts.repository.metadata.adapter;

import org.intelligentsia.artifacts.repository.metadata.Snapshot;
import org.json.JSONException;
import org.json.JSONObject;

/**
 * Snapshot class Adapter.
 * 
 * @author <a href="mailto:jguibert@intelligents-ia.com" >Jerome Guibert</a>
 */
public class SnapshotAdapter {

	public static Snapshot read(JSONObject jsonObject) throws JSONException {
		Snapshot snapshot = null;
		if (jsonObject != null) {
			snapshot = new Snapshot();
			snapshot.setBuildNumber(jsonObject.optInt("buildNumber"));
			snapshot.setLocalCopy(jsonObject.optBoolean("localCopy"));
			snapshot.setTimestamp(jsonObject.getString("timestamp"));
		}
		return snapshot;
	}

	public static JSONObject write(Snapshot snapshot) throws JSONException {
		JSONObject jsonObject = null;
		if (snapshot != null) {
			jsonObject = new JSONObject();
			jsonObject.putOpt("buildNumber", snapshot.getBuildNumber());
			jsonObject.putOpt("localCopy", snapshot.isLocalCopy());
			jsonObject.putOpt("timestamp", snapshot.getTimestamp());
		}
		return jsonObject;
	}
}
